const mysql = require('../../utils/db');
const BaseModel = require('../BaseModel');
const _table_name = "song_list";

exports.findAll = (success, fail, _all = false) => {
    let order = " ORDER BY list_id DESC";
    let sqlStr = `
                SELECT
                    s.*,
                    GROUP_CONCAT(c.NAME ORDER BY s.lable_ids) AS tags
                FROM
                    ${_table_name} s LEFT JOIN classify_tag c on FIND_IN_SET(c.id,s.lable_ids)
                GROUP BY s.list_id  ${order}
                `;
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.findRand = (success, fail) => {
    let order = " ORDER BY RAND() limit 10";
    let sqlStr = `select * from ${_table_name} ${order}`;
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.updateSongList = async (params, updatekey, success, fail) => {
    let sqlStr = BaseModel.update(_table_name, params, updatekey);

    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

/**
 * 条件查询 / 无条件 默认查询所有
 * @param {Object} params 
 * @param {Number} curPage 当前页码  默认1
 * @param {Number} pageSize 一页条数 默认50
 * @returns 
 */
exports.findSongList = async (params, tag = "", curPage = 0, pageSize = 20) => {
    this.data = null;
    this.err = null;
    let sqlStr = `
                SELECT
                    s.*,
                    GROUP_CONCAT(c.NAME ORDER BY s.lable_ids) AS tags
                FROM
                    ${_table_name} s left join classify_tag c on FIND_IN_SET(c.id,s.lable_ids)
                `;
    if (JSON.stringify(params) != '{}' && params || tag != "") {
        sqlStr += " where";

        if (JSON.stringify(params) != '{}')
            sqlStr = BaseModel.findCondition(sqlStr, params);
        if (tag != "") {
            sqlStr += ` find_in_set('${tag}',lable_ids)`
        }
    }

    sqlStr += `GROUP BY s.list_id limit ${curPage},${pageSize}`;
    await mysql.row(sqlStr)
        .then(data => {
            this.data = data;
        })
        .catch(err => {
            this.err = err;
        });

    this.success = (fn) => {
        if (this.data)
            return fn(this.data);
    }
    this.fail = (fn) => {
        if (this.err)
            return fn(this.err);
    }
    return this;
}

exports.delSongList = (params, success, fail) => {
    let sqlStr = BaseModel.delete(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}

exports.addSongList = (params, success, fail) => {
    let sqlStr = BaseModel.add(_table_name, params);
    return mysql.row(sqlStr)
        .then(data => {
            success(data);
        })
        .catch(err => {
            fail(err);
        });
}